package com.example.fireassistant_server.service.impl;

import com.example.fireassistant_server.dto.poi.PoiAddRequest;
import com.example.fireassistant_server.dto.poi.PoiDto;
import com.example.fireassistant_server.dto.poi.PoiUpdateRequest;
import com.example.fireassistant_server.entity.File;
import com.example.fireassistant_server.entity.PoiEntity;
import com.example.fireassistant_server.entity.User;
import com.example.fireassistant_server.exception.BizException;
import com.example.fireassistant_server.exception.ExceptionType;
import com.example.fireassistant_server.mapper.PoiMapper;
import com.example.fireassistant_server.repository.PoiRepository;
import com.example.fireassistant_server.service.FileService;
import com.example.fireassistant_server.service.PoiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class PoiServiceImpl implements PoiService {

    PoiMapper poiMapper;

    PoiRepository poiRepository;

    FileService fileService;

    @Override
    public PoiDto addPoi(PoiAddRequest poiAddRequest, User user) {
        PoiEntity poiEntity = poiMapper.createEntity(poiAddRequest);
        poiEntity.setUser(user);
        return poiMapper.toDto(poiRepository.save(poiEntity));
    }

    @Override
    public PoiDto get(String id) {
        Optional<PoiEntity> poiEntity = poiRepository.findById(id);
        if (!poiEntity.isPresent()) {
            throw new BizException(ExceptionType.POI_NOT_FOUND);
        }
        return poiMapper.toDto(poiEntity.get());
    }


    @Override
    public void delete(String id) {
        Optional<PoiEntity> poiEntity = poiRepository.findById(id);
        if (!poiEntity.isPresent()) {
            throw new BizException(ExceptionType.POI_NOT_FOUND);
        }
        //删除Poi点，需要先删除对应的file
        List<File> fileArrayList = poiEntity.get().getFiles();
        if (!fileArrayList.isEmpty()){
            fileArrayList.forEach(file -> {
                System.out.println(file.getId());
                fileService.delete("poi",file.getId());
            });
        }
        // 删除POI点
        poiRepository.deleteById(id);
    }

    @Override
    public PoiDto update(String id, PoiUpdateRequest poiUpdateRequest) {
        Optional<PoiEntity> poiEntity = poiRepository.findById(id);
        if (!poiEntity.isPresent()) {
            throw new BizException(ExceptionType.POI_NOT_FOUND);
        }
        if (poiUpdateRequest.getName()!=null){
            poiEntity.get().setName(poiUpdateRequest.getName());
        }
        if (poiUpdateRequest.getDescription() != null) {
            poiEntity.get().setDescription(poiUpdateRequest.getDescription());
        }
        return poiMapper.toDto(poiRepository.save(poiEntity.get()));
    }

    @Autowired
    public void setPoiMapper(PoiMapper poiMapper) {
        this.poiMapper = poiMapper;
    }

    @Autowired
    public void setPoiRepository(PoiRepository poiRepository) {
        this.poiRepository = poiRepository;
    }

    @Autowired
    public void setFileService(FileService fileService) {
        this.fileService = fileService;
    }
}
